<?php


namespace app\engine\controller;


use cmf\controller\HomeBaseController;
use think\Db;

class AllController extends HomeBaseController
{
    public function index()
    {
        //所有项目
        //决算总价
        $ht = Db::name('engine')->sum('countPrice');
        $lx = Db::name('contact')->sum('price');
        $bzjf = Db::name('reward')->sum('price');
        $jgcdm = 0;
        $tmp = Db::name('goods_rece')->where(['replace' => 1])->select()->toArray();
        foreach ($tmp as $k => &$v) {
            $jgcdm = +$v['replceNums'] * $v['replcePrice'];
        }

        $coor = Db::name('coor')->sum('price');
        $all = $ht + $lx + $bzjf + $jgcdm + $coor;
        $bill = 0;
        $pay = Db::name('run_bill')->select()->toArray();
        foreach ($pay as $k => $v) {
            $bill += $v['num'] * $v['price'];
        }
        $final = 0;
        $bz = Db::name('final')->where('type', '<>', 3)->select()->toArray();
        foreach ($bz as $K => $v) {
            $final += $v['deciPrice'];
        }
        $pays = $bill + $final;
        $san = $all - $pays;

        $engine = Db::name('engine')->field('id,name,countPrice')->select()->toArray();
        foreach ($engine as  &$v) {
            $v['lxd'] = Db::name('contact')->where(['eid' => $v['id']])->sum('price');
            $v['bzjf'] = Db::name('reward')->where(['eid' => $v['id']])->sum('price');
            $jgcdm = Db::name('goods_rece')->where(['replace' => 1, 'eid' => $v['id']])->select()->toArray();
            $v['jgcdm'] = 0;
            foreach ($jgcdm as $f) {
                $v['jgcdm'] = +$f['replceNums'] * $f['replcePrice'];
            }
            $v['jfbpc'] = Db::name('coor')->where(['eid' => $v['id'], 'type' => 1])->sum('price');
            $v['jfbph'] = Db::name('coor')->where(['eid' => $v['id'], 'type' => 2])->sum('price');
            $v['allPrice'] = $v['lxd'] + $v['bzjf'] + $v['jgcdm'] + $v['jfbpc'] + $v['jfbph'] + $v['countPrice'];
            $byj = Db::name('run_bill')->where(['payfor' => 1, 'eid' => $v['id']])->select()->toArray();
            //流水(备用金)
            $v['byj'] = 0;
            foreach ($byj as $r => $b) {
                $v['byj'] += $b['num'] * $b['price'];
            }
            $pop = Db::name('run_bill')->where(['payfor' => 2, 'eid' => $v['id']])->select()->toArray();
            //流水(签字)
            $v['sign'] = 0;
            foreach ($pop as $r => $w) {
                $v['sign'] += $w['num'] * $w['price'];
            }
            $sv = Db::name('run_bill')->where(['payfor' => 3, 'eid' => $v['id']])->select()->toArray();
            //流水(公司)
            $v['com'] = 0;
            foreach ($sv as $q => $t) {
                $v['com'] += $t['num'] * $t['price'];
            }
            $bzjs = Db::name('final')->where(['type' => 1, 'eid' => $v['id']])->field('deciPrice')->select()->toArray();
            $v['bzjs'] = 0;
            foreach ($bzjs as $ll => &$pp) {
                $v['bzjs'] += $pp['deciPrice'];
            }
            $ghsjs = Db::name('final')->where(['type' => 2, 'eid' => $v['id']])->select()->toArray();
            $v['ghsjs'] = 0;
            foreach ($ghsjs as $oi => $pg) {
                $v['ghsjs'] += $pg['deciPrice'];
            }
            $v['allpay'] = $v['byj'] + $v['sign'] + $v['com'] + $v['bzjs'] + $v['ghsjs'];
            $v['can'] = $v['allPrice'] - $v['allpay'];
        }
            $this->assign('all', $all);
            $this->assign('pays', $pays);
            $this->assign('san', $san);
            $this->assign('engine',$engine);
            return $this->fetch();
        }
    }